home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Sample Code / MacCalendar 1.0d5 / Src / OpenPreferenceFile.c < prev   
Encoding:
C/C++ Source or Header  |  1994-06-30  |  2.7 KB  |  101 lines  |  [TEXT/KAHL]

  1. /*                                OpenPreferenceFile.h                                */
  2. /*
  3.  * OpenPreferenceFile.h
  4.  * Copyright © 1993-94 Apple Computer Inc. All rights reserved.
  5.  */
  6. #include <Errors.h>
  7. #include <GestaltEqu.h>
  8. #include <Folders.h>
  9. #include <Resources.h>
  10. #include <Types.h>
  11. #ifndef FALSE
  12. #define FALSE        0
  13. #define TRUE        1
  14. #endif
  15.  
  16. OSErr                        OpenPreferenceFile(
  17.         OSType                    prefFileType,
  18.         OSType                    prefFileCreator,
  19.         SignedByte                permission,
  20.         short                    *prefVRefNum,
  21.         StringPtr                prefFileName
  22.     );
  23.  
  24. static void ClearMemory(
  25.         Ptr                        dataPtr,
  26.         unsigned long            dataSize
  27.     )
  28. {
  29.         while (dataSize-- > 0)
  30.             *dataPtr++ = 0;
  31. }
  32. #define CLEAR(what)    (ClearMemory((Ptr) &what, sizeof what))
  33.  
  34. /*
  35.  * Open the preference file. Return noErr if successful.
  36.  * Note that the preference file is specified by its file type and creator, not by
  37.  * the file name -- this is so that the user can re-name the file, or so it can
  38.  * be localized without changing the program.
  39.  *
  40.  * This routine does not create the preference file.
  41.  */
  42. OSErr
  43. OpenPreferenceFile(
  44.         OSType                    prefFileType,
  45.         OSType                    prefFileCreator,
  46.         SignedByte                permission,
  47.         short                    *prefVRefNum,        /* Result: resource file refNum    */
  48.         StringPtr                prefFileName        /* Result: resource file name    */
  49.     )
  50. {
  51.         OSErr                    status;
  52.         long                    gestaltResult;
  53.         short                    prefFolderVRefNum;
  54.         long                    prefFolderDirID;
  55.         CInfoPBRec                specPBRec;
  56.         FSSpec                    prefFSSpec;            /* IM-VI 25-15, 25-30        */
  57.         short                    i;
  58. #define SPEC    (specPBRec.hFileInfo)
  59.         
  60.         status = Gestalt(gestaltFindFolderAttr, &gestaltResult);
  61.         if (status == noErr) {                /* We have FindFolder                    */
  62.             status = FindFolder(
  63.                         kOnSystemDisk,
  64.                         kPreferencesFolderType,
  65.                         kDontCreateFolder,
  66.                         &prefFolderVRefNum,
  67.                         &prefFolderDirID
  68.                     );
  69.         }
  70.         if (status == noErr) {                /* We have a Preferences Folder            */
  71.             CLEAR(specPBRec);
  72.             SPEC.ioNamePtr = prefFileName;            /* This will get the file name    */
  73.             SPEC.ioVRefNum = prefFolderVRefNum;
  74.             for (i = 1; status == noErr; i++) {        /* Search for this creator/type    */
  75.                 SPEC.ioDirID = prefFolderDirID;
  76.                 SPEC.ioFDirIndex = i;
  77.                 status = PBGetCatInfo(&specPBRec, FALSE);
  78.                 if (status == noErr
  79.                  && SPEC.ioFlFndrInfo.fdType == prefFileType
  80.                  && SPEC.ioFlFndrInfo.fdCreator == prefFileCreator)
  81.                     break;                            /* Successful: we found it        */
  82.             }
  83.         }
  84.         if (status == noErr) {                /* We have the Preferences file            */
  85.             status = FSMakeFSSpec(
  86.                         prefFolderVRefNum,
  87.                         prefFolderDirID,
  88.                         prefFileName,
  89.                         &prefFSSpec
  90.                     );
  91.         }
  92.         if (status == noErr) {                /* We have a pref file FSSpec            */
  93.             *prefVRefNum = FSpOpenResFile(&prefFSSpec, permission);
  94.             status = ResError();
  95.         }
  96.         if (status != noErr)
  97.             *prefVRefNum = 0;
  98.         return (status);
  99. #undef SPEC
  100.  
  101. }